const router = require('express').Router();
const multer  = require('multer')
const path = require('path');
// 自定义上传目录 文件名
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, './public/uploads')
  },
  filename: function (req, file, cb) {

    cb(null, file.fieldname + '-' + Date.now()+path.extname(file.originalname))
  }
})

const upload = multer({ storage: storage })

// 显示上传文件的页面
router.get('/upload', (req, res) => {
  res.render('upload')
})

// 文件上传路由
router.post('/upload',upload.single('file'), (req, res) => {
  console.log(req.file);
  res.send({
    code: 0,
    msg:'上传成功',
    data: '/static/uploads/'+req.file.filename // 上传成功的服务器的地址 /static/uploads/xxx.xxx
  })
})

module.exports = router